﻿using CounterStore = Microsoft.Isam.Esent.Collections.Generic.PersistentDictionary<string, long>;

namespace TfsGiggle.Core
{
    /// <summary>
    /// Counter is used to update statistics in the CounterStore
    /// </summary>
    public sealed class Counter
    {
        CounterStore _store;
        string _name;

        /// <summary>
        /// A counter is a name/key in the CounterStore
        /// </summary>
        /// <param name="name"></param>
        /// <param name="store"></param>
        public Counter(string name, CounterStore store)
        {
            _name = name;
            _store = store;
            if (!_store.ContainsKey(name))
            {
                _store.Add(name, 0);
            }
        }

        /// <summary>
        /// Add to the counter
        /// </summary>
        /// <param name="numberToAdd"></param>
        /// <returns></returns>
        public long Add(long numberToAdd)
        {
            return _store[_name] += numberToAdd;
        }

        /// <summary>
        /// Set the counter
        /// </summary>
        /// <param name="numberToSet"></param>
        /// <returns></returns>
        public long Set(long numberToSet)
        {
            return _store[_name] = numberToSet;
        }

        /// <summary>
        /// Get the Value
        /// </summary>
        /// <returns></returns>
        public long Value { get { return _store[_name]; } }
    }
}